<?php
/**
 * User to project management page controller
 *
 * @author Dima
 *
 */

class Admin_UsertoprojectController extends modules_admin_controllers_ControllerBase
{
    protected $_bForLoggedUsersOnly = true;
    
//----------------------------------------------------------------------------------------------------
    
    public function init()
    {
        if (! $this->getRequest()->isXmlHttpRequest())
        {
            parent::init();
            $this->view->title = 'User to Project Management';
        }
        
        $this->_helper->AjaxContext()->initContext('json');     
    }
    
//----------------------------------------------------------------------------------------------------

    public function indexAction()
    {
        $this->view->headLink()->appendStylesheet('/css/default.css');
        $this->view->headScript()->appendFile('/js/jquery.tablesorter.js');
        $this->view->headScript()->appendFile('/js/sort.js');     

        $this->view->headLink()->appendStylesheet('/css/ui-lightness/jquery-ui-1.7.2.custom.css');
        $this->view->headScript()->appendFile('/js/lib/jquery-ui.js');
        $this->view->headScript()->appendFile('/js/usertoproject.js');
        
        $oRequest   = $this->getRequest();
        $page       = $oRequest->getParam('page');
        $aItems     = models_UsertoprojectMapper::getAll($page);

        $aUser      = models_UserMapper::fetchAll();
        $aProject   = models_ProjectMapper::getAll(1, 99999999999);
        
        $this->view->paginator  = $aItems['paginator'];       
        $this->view->aUser      = $aUser;       
        $this->view->aProject   = $aProject['array'];       
    }
    
//----------------------------------------------------------------------------------------------------

    public function deleteAction()
    {
    	$oRequest      = $this->getRequest();
    	$id            = $oRequest->getParam('id');
    	list($idUser, $idProject) = explode('_', $id);
    	
//    	echo "$idUser, $idProject"; die;
    	
        try 
        {
            models_UsertoprojectMapper::delete($idUser, $idProject);
        } 
        catch (Exception $e) 
        {
        }
            $this->_redirect('/admin/usertoproject/');
    }
    
//----------------------------------------------------------------------------------------------------

    public function addAction()
    {
        $oRequest               = $this->getRequest();
        
        if ($oRequest->isPost())
        {
            $user               = strip_tags($oRequest->getParam('filter_user'));
            $project            = strip_tags($oRequest->getParam('filter_project'));
            $start_date         = strip_tags($oRequest->getParam('start_date'));            
            $end_date           = strip_tags($oRequest->getParam('end_date'));  
            $endless            = strip_tags($oRequest->getParam('endless'));  
            
            if( ('' != $user) && ('' != $project) && ('' != $start_date) )
            {
                $aData['id_user']       = models_UserMapper::findByEmail($user)->id;
                $aData['id_project']    = models_ProjectMapper::findByName($project)->id;
                $aData['start_date']    = $start_date;
                $aData['end_date']      = $end_date;
                $aData['endless']       = 'on' == $endless ? 1 : 0 ;
                        
                $idItem = models_UsertoprojectMapper::save($aData);
            }
            else
            {
            	die('ololo');
            }
            $this->_redirect('/admin/usertoproject');
        }
        else
        {
//            $this->view->headScript()->appendFile('/js/addcardcategory.js');
        }
    }
    
//----------------------------------------------------------------------------------------------------

    public function editAction()
    {
        $oRequest = $this->getRequest();

        if ($oRequest->isPost())
        {
            $user                = strip_tags($oRequest->getParam('filter_user'));
            $project             = strip_tags($oRequest->getParam('filter_project'));
            $start_date          = strip_tags($oRequest->getParam('start_date'));            
            $end_date            = strip_tags($oRequest->getParam('end_date'));  
            $endless             = strip_tags($oRequest->getParam('endless'));  
            
            $aData['id_user']    = $user;
            $aData['id_project'] = $project;
            $aData['start_date'] = $start_date;
            $aData['end_date']   = $end_date;
            $aData['endless']    = 'on' == $endless ? 1 : 0 ;

            $id     = models_UsertoprojectMapper::findByIdUserAndIdProject($user, $project);
            models_UsertoprojectMapper::edit($id->id, $aData);
            
            $this->_redirect('/admin/usertoproject/');
        }
        else
        {
        	$this->view->headLink()->appendStylesheet('/css/default.css');
	
	        $this->view->headLink()->appendStylesheet('/css/ui-lightness/jquery-ui-1.7.2.custom.css');
	        $this->view->headScript()->appendFile('/js/lib/jquery-ui.js');
	        $this->view->headScript()->appendFile('/js/usertoproject.js');
	        
            list($idUser, $idProject) = explode('_', strip_tags($oRequest->getParam('id')));
            $id                       = models_UsertoprojectMapper::findByIdUserAndIdProject($idUser, $idProject);
            $aUser                    = models_UserMapper::fetchAll();
            $aProject                 = models_ProjectMapper::getAll(1, 99999999999);
        
            $this->view->aUser        = $aUser;       
            $this->view->aProject     = $aProject['array'];   
            $this->view->val          = $id;
        }
    }
    
//----------------------------------------------------------------------------------------------------
     
}